﻿/* 
 * Copyright (c) 2009 Intel Corporation
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * -- Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * -- Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * -- Neither the name of the Intel Corporation nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE INTEL OR ITS
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * Contributor: Nathan Letwory
 */

using System;

namespace VerseSharp
{

    public delegate void ObjectMethodCallCallback(ObjectMethodCallCommand command);

    public class ObjectMethodCallCommand : IVerseCommand
    {
        public CommandID ID { get { return CommandID.ObjectMethodCall; } }
        public uint NodeID;
        /// <summary>
        /// ID of the method group.
        /// </summary>
        public ushort GroupID;
        public ushort MethodID;
        public uint SenderID;
        public byte[] PackedParams;
        public MethodParameter[] Params;

        public void UnpackParameters()
        {
            int bufpos = 2;
            for (int i = 0; i < Params.Length; i++)
            {
                switch (Params[i].Type)
                {
                    case ParamType.Int8:
                        Params[i].Value.vint8 = (int)Utils.GetInt8(PackedParams, ref bufpos);
                        break;
                    case ParamType.Int16:
                        Params[i].Value.vint16 = Utils.GetInt16(PackedParams, ref bufpos);
                        break;
                    case ParamType.Int32:
                        Params[i].Value.vint32 = Utils.GetInt32(PackedParams, ref bufpos);
                        break;
                    case ParamType.Uint8:
                        Params[i].Value.vuint8 = (uint)Utils.GetUInt8(PackedParams, ref bufpos);
                        break;
                    case ParamType.Uint16:
                        Params[i].Value.vuint16 = Utils.GetUInt16(PackedParams, ref bufpos);
                        break;
                    case ParamType.Uint32:
                        Params[i].Value.vuint32 = Utils.GetUInt32(PackedParams, ref bufpos);
                        break;
                    case ParamType.Real32:
                        Params[i].Value.vreal32 = Utils.GetReal32(PackedParams, ref bufpos);
                        break;
                    case ParamType.Real64:
                        Params[i].Value.vreal64 = Utils.GetReal64(PackedParams, ref bufpos);
                        break;
                    case ParamType.Node:
                        Params[i].Value.vnode = Utils.GetUInt32(PackedParams, ref bufpos);
                        break;
                    case ParamType.Layer:
                        Params[i].Value.vlayer = Utils.GetUInt16(PackedParams, ref bufpos);
                        break;
                    case ParamType.String:
                        Params[i].Value.vstring = Utils.GetString(PackedParams, PackedParams.Length - bufpos, ref bufpos);
                        break;
                    default:
                        // TODO: implement decoding for vectors and matrices. For now, let's just 'break' when unpacking ;)
                        break;
                }
            }
        }

        public int PackParameters()
        {
            byte[] buf = new byte[1500 + 8*16]; // no idea yet why this is used in reference implementation. It's not really documented :S
            int bufpos = 0;
            Utils.PutUInt16(buf, ref bufpos, 0);
            for (int i = 0; i < Params.Length; i++)
            {
                switch (Params[i].Type)
                {
                    case ParamType.Int8:
                        Utils.PutInt8(buf, ref bufpos, (byte)Params[i].Value.vint8);
                        break;
                    case ParamType.Int16:
                        Utils.PutInt16(buf, ref bufpos, Params[i].Value.vint16);
                        break;
                    case ParamType.Int32:
                        Utils.PutInt32(buf, ref bufpos, Params[i].Value.vint32);
                        break;
                    case ParamType.Uint8:
                        Utils.PutUInt8(buf, ref bufpos, (byte)Params[i].Value.vuint8);
                        break;
                    case ParamType.Uint16:
                        Utils.PutUInt16(buf, ref bufpos, Params[i].Value.vuint16);
                        break;
                    case ParamType.Uint32:
                        Utils.PutUInt32(buf, ref bufpos, Params[i].Value.vuint32);
                        break;
                    case ParamType.Real32:
                        Utils.PutReal32(buf, ref bufpos, Params[i].Value.vreal32);
                        break;
                    case ParamType.Real64:
                        Utils.PutReal64(buf, ref bufpos, Params[i].Value.vreal64);
                        break;
                    case ParamType.Node:
                        Utils.PutUInt32(buf, ref bufpos, Params[i].Value.vnode);
                        break;
                    case ParamType.Layer:
                        Utils.PutUInt16(buf, ref bufpos, Params[i].Value.vlayer);
                        break;
                    case ParamType.String:
                        //Params[i].Value.vstring = Utils.GetString(buf, 1500 - bufpos, ref bufpos);
                        Utils.PutString(buf, Params[i].Value.vstring, 1500 - bufpos, ref bufpos);
                        break;
                    default:
                        // TODO: implement decoding for vectors and matrices. For now, let's just 'break' when unpacking ;)
                        break;
                }
            }
            // now encode length in byte block
            DataConverter.BigEndian.PutBytes(buf, 0, (UInt16)bufpos);

            // copy buf to packed bytes
            if (bufpos > 0)
            {
                PackedParams = new byte[bufpos];
                Buffer.BlockCopy(buf, 0, PackedParams, 0, bufpos);
            }

            return bufpos;

        }

        public void Deserialize(byte[] source, ref int pos)
        {
            NodeID = Utils.GetUInt32(source, ref pos);
            GroupID = Utils.GetUInt16(source, ref pos);
            MethodID = Utils.GetUInt16(source, ref pos);
            SenderID = Utils.GetUInt32(source, ref pos);

            ushort length = DataConverter.BigEndian.GetUInt16(source, pos);
            PackedParams = new byte[length];
            Buffer.BlockCopy(source, pos, PackedParams, 0, length);
            pos += length;
        }

        public void Serialize(byte[] destination, ref int pos)
        {
            Utils.PutUInt32(destination, ref pos, NodeID);
            Utils.PutUInt16(destination, ref pos, GroupID);
            Utils.PutUInt16(destination, ref pos, MethodID);
            Utils.PutUInt32(destination, ref pos, SenderID);
            if (Params == null || Params.Length == 0)
            {
                // empty call
                PackedParams = new byte[2];
                DataConverter.BigEndian.PutBytes(PackedParams, 0, PackedParams.Length);
            }
            else
            {
                int length = PackParameters();
            }
            Buffer.BlockCopy(PackedParams, 0, destination, pos, PackedParams.Length);
            pos += PackedParams.Length;
        }
    }
}
